<?php
function cate($var)
{
    //dump($var);
    $proall = M('user')->where(array('UE_accName' => $var, 'UE_Faccount' => '0', 'UE_check' => '1', 'UE_stop' => '1'))->count("UE_ID");
    return $proall;
}


function sfjhff($r)
{
    $a = array("未激活", "已激活");
    return $a[$r];
}


function getRand($proArr)
{
    $result = '';

    //概率数组的总概率精度
    $proSum = array_sum($proArr);

    //概率数组循环
    foreach ($proArr as $key => $proCur) {
        $randNum = mt_rand(1, $proSum);
        if ($randNum <= $proCur) {
            $result = $key;
            break;
        } else {
            $proSum -= $proCur;
        }
    }
    unset ($proArr);

    return $result;
}


function getpage($count, $pagesize = 10)
{
    $p = new Think\Page($count, $pagesize);
    $p->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
    $p->setConfig('prev', '上一页');
    $p->setConfig('next', '下一页');
    $p->setConfig('last', '末页');
    $p->setConfig('first', '首页');
    $p->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
    $p->lastSuffix = false;//最后一页不显示为总页数
    return $p;
}

function cx_user($var)
{
    //dump($var);
    $proall = M('user')->where(array('UE_account' => $var))->find();
    return $proall['ue_theme'];
}


function diffBetweenTwoDays($day1, $day2)
{
    $second1 = strtotime($day1);
    $second2 = strtotime($day2);

    if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
    }
    return ($second1 - $second2) / 86400;
}


function user_jj_lx($var)
{
    $proall = M('user_jj')->where(array('id' => $var))->find();//加入查询
    $result = M("userget")->where(array("varid" => $var))->find();//提现查询
    $ppdd = M("ppdd")->where(array("id" => $proall["r_id"]))->find();//配对信息
    $NowTime = date('Y-m-d', strtotime($proall['date']));//格式化加入时间
    $NowTime2 = date('Y-m-d', strtotime($ppdd["date"]));//格式化配对时间
    if (!empty($result)) {//如果已经提现，则计算加入日期到提现日的利息
        $NowTime3 = date("Y-m-d", strtotime($result["UG_getTime"]));//格式化提现时间
    } else {
        $nowtime3 = date('Y-m-d', time());//格式化当前日期
    }
    $diff1 = diffBetweenTwoDays($NowTime, $NowTime2);//计算加入时间到配对时间的天数
    $diff2 = diffBetweenTwoDays($NowTime2, $nowtime3);//计算配对时间到提现的天数
    if ($diff1 >= C("jjfhdays")) {
        $diff1 = C("jjfhdays");
    }
    if ($diff2 >= C("jjfhdays")) {
        $diff2 = C("jjfhdays");
    }
    //return $diff2;
    return ($proall['jb'] * $diff1 * (intval(C("lixi1")) / 100)) + ($proall['jb'] * $diff2 * (intval(C("lixi2")) / 100));
//
//    $proall = M('user_jj')->where(array('id' => $var))->find();
//    //date('Y-m-d H:i:s',$dayBegin);
//    $NowTime = $proall['date'];
//    $aab = strtotime($NowTime);
//    $NowTime = date('Y-m-d', $aab);
//    $NowTime2 = date('Y-m-d', time());
//    $diff = diffBetweenTwoDays($NowTime, $NowTime2);
//    if ($diff > 10) {
//        $diff = 10;
//    }
//    return $proall['jb'] * $diff * (intval(C("lixi1")) / 100);
}

function user_jj_lx_jerry($var)
{
    $tgbz = M('tgbz')->where(array('id' => $var))->find();//加入查询
    $NowTime = date('Y-m-d', strtotime($tgbz['date']));//格式化加入时间
    $NowTime2 = date('Y-m-d', time());//格式化当前日期
    $diff1 = diffBetweenTwoDays($NowTime, $NowTime2);//计算加入时间到配对时间的天数
    if ($diff1 >= C("jjfhdays")) {
        $diff1 = C("jjfhdays");
    }
    //return $diff2;
    return $tgbz['jb'] * $diff1 * (intval(C("lixi1")) / 100);
}



function user_jj_ts($var)
{

    $proall = M('user_jj')->where(array('id' => $var))->find();

    //date('Y-m-d H:i:s',$dayBegin);
    $NowTime = $proall['date'];
    $aab = strtotime($NowTime);
    $NowTime = date('Y-m-d', $aab);
    $result = M("userget")->where(array("varid" => $var))->getField("ug_gettime");
    if (!empty($result)) {
        $NowTime2 = date("Y-m-d", strtotime($result));
    } else {
        $NowTime2 = date('Y-m-d', time());
    }
    $day1 = $NowTime;
    $day2 = $NowTime2;
    $diff = diffBetweenTwoDays($day1, $day2);
    if ($diff >= C("jjfhdays")) {
        $diff = C("jjfhdays");
    }
    //$diff = $diff/100;
    return $diff;

}

function user_jj_ts_jerry($var)
{

    $proall = M('tgbz')->where(array('id' => $var))->find();

    //date('Y-m-d H:i:s',$dayBegin);
    $NowTime = $proall['date'];
    $aab = strtotime($NowTime);
    $NowTime = date('Y-m-d', $aab);
    $NowTime2 = date('Y-m-d', time());
    $day1 = $NowTime;
    $day2 = $NowTime2;
    $diff = diffBetweenTwoDays($day1, $day2);
    if ($diff >= C("jjfhdays")) {
        $diff = C("jjfhdays");
    }
    //$diff = $diff/100;
    return $diff;

}

function user_tgbz_jerry($id)
{
    $result = M("userget")->where(array("varid" => $id))->find();
    if ($result) {
        return "已转出";
    } else {
        return "未转出";
    }
}

function user_jj_tx($var)
{

    $proall = M('tgbz')->where(array('id' => $var))->find();

    //date('Y-m-d H:i:s',$dayBegin);
    $NowTime = $proall['date'];
    $aab = strtotime($NowTime);
    $NowTime = date('Y-m-d', $aab);
    $NowTime2 = date('Y-m-d', time());


    $day1 = $NowTime;
    $day2 = $NowTime2;
    return $diff = diffBetweenTwoDays($day1, $day2);

}


function user_jj_sj($var)
{

    $proall = M('tgbz')->where(array('id' => $var))->find();

    $user = M('user')->where(array(UE_account => $proall ['user']))->find();
    return $user['ue_phone'];

}


function user_jj_tx1($var)
{

    $proall = M('jsbz')->where(array('id' => $var))->find();

    //date('Y-m-d H:i:s',$dayBegin);
    $NowTime = $proall['date'];
    $aab = strtotime($NowTime);
    $NowTime = date('Y-m-d', $aab);
    $NowTime2 = date('Y-m-d', time());


    $day1 = $NowTime;
    $day2 = $NowTime2;
    return $diff = diffBetweenTwoDays($day1, $day2);

}


function user_jj_sj1($var)
{

    $proall = M('jsbz')->where(array('id' => $var))->find();

    $user = M('user')->where(array(
        UE_account => $proall ['user']
    ))->find();
    return $user['ue_phone'];

}


function user_jj_zt($var)
{

    $proall = M('user_jj')->where(array('id' => $var))->find();
    $proall2 = M('ppdd')->where(array('id' => $proall['r_id']))->find();
    //date('Y-m-d H:i:s',$dayBegin);
    $NowTime = $proall['date'];
    $aab = strtotime($NowTime);
    $NowTime = date('Y-m-d', $aab);
    $NowTime2 = date('Y-m-d', time());


    $day1 = $NowTime;
    $day2 = $NowTime2;
    $diff = diffBetweenTwoDays($day1, $day2);
    if ($diff >= 0 && $proall2['zt'] == '2') {
        return '1';
    } else {
        return '0';;
    }
}


function user_jj_zt_z($var)
{

    if (user_jj_zt($var) == '1') {
            $User = M('user_jj'); // 實例化User對象
            $list1 = $User->where("id='$var'")->find();
            $successtime = strtotime($list1['date']);
            $nowtime = time();
            $jjdjdays = C('jjdjdays');
            $jjdtime = $jjdjdays*3600*24;
            if($nowtime<$jjdtime+$successtime)
            {
                 return '不可提现';
            }
        return '可以提现';
    } else {
        return '不可提现';
    }
}

function datedqsjss($var)
{
   $User = M('user_jj'); // 實例化User對象
    $list1 = $User->where("id='$var'")->find();
    $successtime = strtotime($list1['date']);
    $nowtime = time();
    $jjdjdays = C('jjdjdays');
    $jjdtime = $jjdjdays*3600*24;
    $aab2 = $jjdtime+$successtime;
    return date('Y-m-d H:i:s', $aab2);;

}

function user_jj_pipei_z($var)
{
    $proall = M('ppdd')->where(array('id' => $var))->find();
    if ($proall['zt'] == '0') {
        return '未打款';
    } elseif ($proall['zt'] == '1') {
        return '已打款';
    } elseif ($proall['zt'] == '2') {
        return '交易成功';
    }
}


function user_jj_pipei_z2($var)
{
    $proall = M('ppdd')->where(array('id' => $var))->find();
    if ($proall['zt'] == '0') {
        return '未发放';
    } elseif ($proall['zt'] == '1') {
        return '未发放';
    } elseif ($proall['zt'] == '2') {
        return '已发放';
    }
}


function jlj($a, $b, $c)
{
    jlsja($a);
    $tgbz_user_xx = M('user')->where(array('UE_account' => $a))->find();
    //echo $ppddxx['p_id'];die;
    if ($tgbz_user_xx['sfjl'] == 1) {
        $money = $b;
        $accname_zq = M('user')->where(array('UE_account' => $tgbz_user_xx['ue_account']))->find();
        M('user')->where(array('UE_account' => $tgbz_user_xx['ue_account']))->setInc('jl_he', $money);
        $accname_xz = M('user')->where(array('UE_account' => $tgbz_user_xx['ue_account']))->find();

		
        $record3 ["UG_account"] = $tgbz_user_xx['ue_account']; // 登入轉出賬戶
        $record3 ["UG_type"] = 'jb';
        $record3 ["UG_allGet"] = $accname_zq['jl_he']; // 金幣
        $record3 ["UG_money"] = '+' . $money; //
        $record3 ["UG_balance"] = $accname_xz['jl_he']; // 當前推薦人的金幣餘額
        $record3 ["UG_dataType"] = 'jlj'; // 金幣轉出
        $record3 ["UG_note"] = $c; // 推薦獎說明
        $record3["UG_getTime"] = date('Y-m-d H:i:s', time()); //操作時間
        $reg4 = M('userget')->add($record3);
    }
    return $tgbz_user_xx['zcr'];

}


function jlj2($a, $b, $c, $d, $e)
{
    $tgbz_user_xx = M('user')->where(array('UE_account' => $a))->find();
    if ($tgbz_user_xx['sfjl'] == 1) {
        $ppddxx = M('ppdd')->where(array('id' => $e))->find();
        $peiduidate = M('tgbz')->where(array('id' => $ppddxx['p_id'], 'user' => $ppddxx['p_user']))->find();
        $data2['user'] = $a;
        $data2['r_id'] = $ppddxx['id'];
        $data2['date'] = $peiduidate['date'];
        $data2['note'] = '经理奖第' . $d . '代';
        $data2['jb'] = $ppddxx['jb'];
        $data2['jj'] = $b;
        $data2['ds'] = $d;
        M('user_jl')->add($data2);
    }
    return $tgbz_user_xx['zcr'];
}

function jlj3($a, $b, $c, $d, $e)
{
    $tgbz_user_xx = M('user')->where(array('UE_account' => $a))->find();
    $ppddxx = M('ppdd')->where(array('id' => $e))->find();
    $peiduidate = M('tgbz')->where(array('id' => $ppddxx['p_id'], 'user' => $ppddxx['p_user']))->find();
    $data2['user'] = $a;
    $data2['r_id'] = $ppddxx['id'];
    $data2['date'] = $peiduidate['date'];
    $data2['note'] = '推荐奖'.C("jjtuijianrate").'%';
    $data2['jb'] = $ppddxx['jb'];
    $data2['jj'] = $b;
    $data2['ds'] = $d;
    M('user_jl')->add($data2);
    return $tgbz_user_xx['zcr'];
}

function newuserjl($user, $b, $c)
{

    $data2['user'] = $user;
    $data2['r_id'] = '0';
    $data2['date'] = date('Y-m-d H:i:s',time());
    $data2['note'] = $c;
    $data2['jb'] = $b;
    $data2['jj'] = $b;
    $data2['ds'] = '0';
    M('user_jl')->add($data2);
	M('user')->where(array(UE_account => $user))->setInc('UE_money', $b);
}

function jlj4($a, $b)
{
    $tgbz_user_xx = M('user')->where(array('UE_account' => $a))->find();

    M('user')->where(array(UE_account => $a))->setInc('tj_he1', $b);


    return $tgbz_user_xx['zcr'];
}

function jlj5($a, $b)
{
    $tgbz_user_xx = M('user')->where(array('UE_account' => $a))->find();
    if ($tgbz_user_xx['sfjl'] == 1) {
        M('user')->where(array(UE_account => $a))->setInc('jl_he1', $b);
    }

    return $tgbz_user_xx['zcr'];
}

function datedqsj($var)
{
$jjdktime=C("jjdktime");

    $NowTime = $var;
    $aab = strtotime($NowTime);
    //$aab2 = $aab + 86400 + 86400;
$aab2 = $aab + 3600 *$jjdktime;

    return date('Y-m-d H:i:s', $aab2);;

}

function hk($var)
{


    return $var . 'RMB';

}

function datedqsj2($var)
{


    $NowTime = $var;
    $aab = strtotime($NowTime);
    $aab2 = $aab + 86400 + 86400;
    $bba3 = date('Y-m-d H:i:s', time());
    $bba4 = strtotime($bba3);

    if ($aab2 > $bba4) {
        return "style='display:none;'";
    }
}

function datedqsj3($var)
{


    $NowTime = $var;
    $aab = strtotime($NowTime);
    $aab2 = $aab + 86400 + 86400;
    $bba3 = date('Y-m-d H:i:s', time());
    $bba4 = strtotime($bba3);

    if ($aab2 > $bba4) {
        return "style='display:none;'";
    }
}

//===2015/11/30 星期一 QQ54885782 可以计算会员级别，升级
function jlsja($var)
{

	/*
    $zctj = 0;
    $zctjuser = M('user')->where(array('UE_accName' => $var))->select();
    foreach ($zctjuser as $value) {
        $tgbztj1 = M('ppdd')->where(array('p_user' => $value['ue_account'], 'zt' => '2'))->sum('jb');
        if ($tgbztj1 >= 700) {
            $zctj++;
        }
    }

    $tgbztj = M('ppdd')->where(array('p_user' => $var, 'zt' => '2'))->sum('jb');

    if ($zctj >= 10 && $tgbztj >= 7000) {
        M('user')->where(array('UE_account' => $var))->save(array('sfjl' => 1));
    }*/
}
//===2015/12/1 QQ54885782 add
/*更新推荐会员数量*/
function mmtjrennumadd($var)
{
	M('user')->where(array('UE_account' => $var))->setInc('tj_num',1);
	$zctjuser = M('user')->where(array('UE_account' => $var))->select();
	foreach ($zctjuser as $value) {
		if($value['ue_accname']<>''){
			//mmtjrennumadd($value['ue_accname']);
		}else{
			return true;
		}
	}
}
function accountaddlevel($var){
	$usermm=M('user')->where(array('UE_account'=>$var))->find();
	$numtemparr = explode(',',C("jjaccountnum"));
	$nametemparr = explode(',',C("jjaccountlevel"));
	$levelnum=0;
	foreach($numtemparr as $k=>$num){
		if($usermm['tj_num']>=$num){
			$levelnum=$k;
		}
	}
	$levelname = $nametemparr[$levelnum];
	M('user')->where(array('UE_account' => $var))->save(array('levelname' => $levelname));
}

function fftuijianmoney($var,$money,$level){
	$tjratearr = explode(',',C("jjtuijianratenew"));
	$tjmoney = ($money*$tjratearr[$level-1])/100;
	$accname_zq=M('user')->where(array('UE_account'=>$var))->find();
	M('user')->where(array('UE_account'=>$var))->setInc('tj_he',$tjmoney);
	$accname_xz=M('user')->where(array('UE_account'=>$var))->find();

			$note3 = "推荐奖".$tjratearr[$level-1]."%";
			$record3 ["UG_account"] = $var; // 登入转出账户
			$record3 ["UG_type"] = 'jb';
			$record3 ["UG_allGet"] = $accname_zq['tj_he']; // 金币
			$record3 ["UG_money"] = '+'.$tjmoney; //
			$record3 ["UG_balance"] = $accname_xz['tj_he']; // 当前推荐人的金币馀额
			$record3 ["UG_dataType"] = 'tjj'; // 金币转出
			$record3 ["UG_note"] = $note3; // 推荐奖说明
			$record3["UG_getTime"] = date ( 'Y-m-d H:i:s', time () ); //操作时间
			$reg4 = M ( 'userget' )->add ( $record3 );

	//jsaccountmoney($var,$money,$accname_xz['levelname']);
	if($accname_xz['ue_accname']<>''){
		//fftuijianmoney($accname_xz['ue_accname'],$money,$level+1);
	}else{
		return true;
	}

}

function jsaccountmoney($account,$money,$levelname){
	if(C("jjaccountflag")=='1'){
		$accountratearr = explode(',',C("jjaccountrate"));
		$nametemparr = explode(',',C("jjaccountlevel"));
		$levelnum=0;
		foreach($nametemparr as $k=>$name){
			if($levelname==$name){
				$levelnum=$k;
			}
		}
		$levelrate = $accountratearr[$levelnum];
		$jjmoney = ($money*$levelrate)/100;

			$accname_zq = M('user')->where(array('UE_account' => $account))->find();
			M('user')->where(array('UE_account' => $account))->setInc('jl_he', $jjmoney);
			$accname_xz = M('user')->where(array('UE_account' => $account))->find();

			$note = '经理级别奖'.$levelrate.'%';
			$record3 ["UG_account"] = $account; // 登入轉出賬戶
			$record3 ["UG_type"] = 'jb';
			$record3 ["UG_allGet"] = $accname_zq['jl_he']; // 金幣
			$record3 ["UG_money"] = '+' . $jjmoney; //
			$record3 ["UG_balance"] = $accname_xz['jl_he']; // 當前推薦人的金幣餘額
			$record3 ["UG_dataType"] = 'jlj'; // 金幣轉出
			$record3 ["UG_note"] = $note; // 推薦獎說明
			$record3["UG_getTime"] = date('Y-m-d H:i:s', time()); //操作時間
			$reg4 = M('userget')->add($record3);
	}
}

function dakuanshijianjl($p_user,$pipeitime,$daikuantime,$money){
	$pipeitime = strtotime($pipeitime);
	$daikuantime = strtotime($daikuantime);
	$difftime = $daikuantime - $pipeitime;
	if( $difftime < (C("jjdkwhattime") * 3600)){
		$ffmoney = $money * C("jjdkwhattimejl") * 0.01;
		$accname_zq=M('user')->where(array('UE_account'=>$p_user))->find();
		M('user')->where(array('UE_account'=>$p_user))->setInc('UE_money',$ffmoney);
		$accname_xz=M('user')->where(array('UE_account'=>$p_user))->find();
			$note3 = C("jjdkwhattime")."小时内打款奖励".C("jjdkwhattimejl")."%";
			$record3 ["UG_account"] = $p_user; // 登入转出账户
			$record3 ["UG_type"] = 'jb';
			$record3 ["UG_allGet"] = $accname_zq['ue_money']; // 金币
			$record3 ["UG_money"] = '+'.$ffmoney; //
			$record3 ["UG_balance"] = $accname_xz['ue_money']; // 当前推荐人的金币馀额
			$record3 ["UG_dataType"] = 'tjj'; // 金币转出
			$record3 ["UG_note"] = $note3; // 说明
			$record3["UG_getTime"] = date ( 'Y-m-d H:i:s', time () ); //操作时间
			$reg4 = M ( 'userget' )->add ( $record3 );
	}else{
		$ffmoney = $money * C("jjdkwhattimecf") * 0.01;
		$accname_zq=M('user')->where(array('UE_account'=>$p_user))->find();
		M('user')->where(array('UE_account'=>$p_user))->setDec('UE_money',$ffmoney);
		$accname_xz=M('user')->where(array('UE_account'=>$p_user))->find();
			$note3 = C("jjdkwhattime")."小时内打款惩罚".C("jjdkwhattimecf")."%";
			$record3 ["UG_account"] = $p_user; // 登入转出账户
			$record3 ["UG_type"] = 'jb';
			$record3 ["UG_allGet"] = $accname_zq['ue_money']; // 金币
			$record3 ["UG_money"] = '-'.$ffmoney; //
			$record3 ["UG_balance"] = $accname_xz['ue_money']; // 当前推荐人的金币馀额
			$record3 ["UG_dataType"] = 'tjj'; // 金币转出
			$record3 ["UG_note"] = $note3; // 说明
			$record3["UG_getTime"] = date ( 'Y-m-d H:i:s', time () ); //操作时间
			$reg4 = M ( 'userget' )->add ( $record3 );
	}

}
//===end

function lkdsjfsdfj($var1, $jb)
{

    $ppddxx['p_user'] = $var1;
    $ppddxx['jb'] = $jb;
    //经理奖金订单
    $tgbz_user_xx = M('user')->where(array('UE_account' => $ppddxx['p_user']))->find();//充值人详细
    //jlj4($tgbz_user_xx['ue_accname'], $ppddxx['jb'] * 0.1);

   /*  if ($tgbz_user_xx['zcr'] <> '') {
        $zcr2 = jlj5($tgbz_user_xx['zcr'], $ppddxx['jb'] * 0.05);
        if ($zcr2 <> '') {
            $zcr3 = jlj5($zcr2, $ppddxx['jb'] * 0.03);
            //echo $ppddxx['p_user'].'sadfsaf';die;
            if ($zcr3 <> '') {
                $zcr4 = jlj5($zcr3, $ppddxx['jb'] * 0.01);
                if ($zcr4 <> '') {
                    $zcr5 = jlj5($zcr4, $ppddxx['jb'] * 0.005);
                    if ($zcr5 <> '') {
                        $zcr6 = jlj5($zcr5, $ppddxx['jb'] * 0.003);
                        if ($zcr6 <> '') {
                            $zcr7 = jlj5($zcr6, $ppddxx['jb'] * 0.001);
                            if ($zcr7 <> '') {
                                $zcr8 = jlj5($zcr7, $ppddxx['jb'] * 0.0005);
                                if ($zcr8 <> '') {
                                    $zcr9 = jlj5($zcr8, $ppddxx['jb'] * 0.0003);

                                    if ($zcr9 <> '') {
                                        jlj5($zcr9, $ppddxx['jb'] * 0.0001);


                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
     }
	*/
    //经理奖金订单

}

/**
* 三级分销
* $ppid 交易订单ID
*/
function get_distribution($ppid){
	
	if(!empty($ppid)){
		
		/*查询交易信息*/
		$ppddxx=M('ppdd')->where(array('id'=>$ppid))->find();
		
		/*查询用户信息 aabb@qq.com*/
		$users = M('user')->where(array('UE_account'=>$ppddxx['p_user']))->find();
		
		/*会员等级 三级分销比例*/
		$jjaccountnum = explode(',',C("jjaccountnum"));
		$levelone = $jjaccountnum[1];//铜牌会员 推荐人数5
		$leveltwo = $jjaccountnum[2];//银牌会员 推荐人数20
		$levelthree = $jjaccountnum[3];//金牌会员 推荐人数50
		
		$onelevel = C("onelevel")*0.01;//一级分销比例
		$twolevel = C("twolevel")*0.01;//二级分销比例
		$threelevel = C("threelevel")*0.01;//三级分销比例	
		
		/*一级分销*/
		if(!empty($users['ue_accname'])){
			
			/*查询上一级用户信息 12345@qq.com*/
			$oneuser = M('user')->where(array('UE_account'=>$users['ue_accname']))->find();
			
			/*计算佣金发放比例*/
			if($oneuser['t_num'] >= $levelone && $oneuser['ue_status'] != '1'){
				
				$one_comm = 0;
				$one_comm = $ppddxx['jb']*$onelevel;
				
				/*发放分销佣金 ot_user_tj*/
				grant_commission($oneuser['ue_account'],$one_comm,"one",$ppid);
				
			}
			
			
			/*二级分销*/
			if(!empty($oneuser['ue_accname'])){
				
				/*查询上上一级用户信息 admin@qq.com*/
				$twouser = M('user')->where(array('UE_account'=>$oneuser['ue_accname']))->find();
				
				/*计算佣金发放比例*/
				if($twouser['t_num'] >= $leveltwo && $twouser['ue_status'] != '1'){
					
					$two_comm = 0;
					$two_comm = $ppddxx['jb']*$twolevel;
					
					/*发放分销佣金 ot_user_tj*/
					grant_commission($twouser['ue_account'],$two_comm,"two",$ppid);
					
				}
				
				
				/*三级分销*/
				if(!empty($twouser['ue_accname'])){
					
					/*查询上上上一级用户信息 admin@qq.com*/
					$threeuser = M('user')->where(array('UE_account'=>$oneuser['ue_accname']))->find();
					
					/*计算佣金发放比例*/
					if($threeuser['t_num'] >= $levelthree && $threeuser['ue_status'] != '1'){
						
						$three_comm = 0;
						$three_comm = $ppddxx['jb']*$threelevel;
						
						/*发放分销佣金 ot_user_tj*/
						grant_commission($threeuser['ue_account'],$three_comm,"three",$ppid);
						
					}
					
				}
				
			}
				
		}
	
	}

}



/**
* 发给佣金
* $name  用户名
* $money 佣金金额
* $type  级别  one一级 two二级 three三级
* $ppid  交易订单ID
**/
function grant_commission($name,$money,$type,$ppid){
	
	if($name){
		
		/*查询交易信息*/
		$ppddxx=M('ppdd')->where(array('id'=>$ppid))->find();
		
		/*发放佣金 更新用户账户*/	
		M('user')->where(array('UE_account' => $name))->setInc('tj_he', $money);	
		
		/*写入记录log */
		$note = "";
		if($type == 'one'){
			$note = "获得一级分销用户佣金";
		}elseif($type == 'two'){
			$note = "获得二级分销用户佣金";
		}elseif($type == 'three'){
			$note = "获得三级分销用户佣金";
		}
		
		$data['user'] = $name;
		$data['pp_id'] = $ppid;
		$data['date'] = date('Y-m-d H:i:s', time()); //操作時間
		$data['note'] = $note;
		$data['jb']   = $ppddxx['jb'];
		$data['money'] = $money;
		$data['type'] = $type;
		M('user_tj')->add($data);	
			
	}	
	
}


/*首推奖 UE_money 用户余额  jl_he(流动) jl_he1(待定) 经理奖金额   tj_he(流动) tj_he1(待定) 推荐奖金额  */
function first_recommend($pp_id){
	
	/*查询提供帮助的用户是否有推荐人*/
	if(!empty($pp_id)){
		
		/*查询交易信息*/
		$ppddxx=M('ppdd')->where(array('id'=>$pp_id))->find();

		/*查询用户信息*/
		$users = M('user')->where(array('UE_account'=>$ppddxx['p_user']))->find();
	
		/*是否已经有推荐人 并且没有发过首推奖*/
		if(!empty($users['ue_accname']) && $users['first_tui'] == 0){
		
			/*发放首推奖*/
			$t_money = 0;
			$t_money = $ppddxx['jb']*C("firsttuijian")*0.01;
			
			M('user')->where(array('UE_account' => $users['ue_accname']))->setInc('tj_he', $t_money);
			
			/*并写入账户资金变动记录*/
			$data['user'] = $users['ue_accname'];
			$data['r_id'] = $pp_id;
			$data['date'] = date('Y-m-d H:i:s', time()); //操作時間
			$data['note'] = '推荐用户'.$ppddxx['p_user'].'的首推奖励';
			$data['jb'] = $t_money;
			$data['jj'] = $t_money;
			M('user_jl')->add($data);

			
			/*更新状态*/
			M('user')->where(array('UE_account' => $ppddxx['p_user']))->save(array('first_tui'=>'1'));

		}
		
	}
	
}



/*短信*/
function sendsmg($telphone, $content)
{
	if($telphone){
		
		//短信接口用户名 $uid
		$uid = C("jjmsguid");
		//短信接口密码 $passwd
		$passwd = C("jjmsgpwd");
		$content = iconv( "UTF-8", "gb2312//IGNORE" , $content);
		$gateway = "http://sdk.zhongguowuxian.com:98/ws/batchSend.aspx?CorpID={$uid}&Pwd={$passwd}&Mobile={$telphone}&Content={$content}&Cell=&SendTime=";
		//echo $gateway;exit;
		$result = file_get_contents($gateway);
		//echo $result;exit;
		if(  $result == 0 || $result == 1)
		{
			return 'suc';
			//echo "发送成功! 发送时间".date("Y-m-d H:i:s");
		}
		else
		{
			return 'fal';
			//echo "发送失败, 错误提示代码: ".$result;
		}
	}
	return 'fal';
}


/*判断身份证是否正确*/
function validation_filter_id_card($id_card){
    if(strlen($id_card)==18){
        return idcard_checksum18($id_card);
    }elseif((strlen($id_card)==15)){
        $id_card=idcard_15to18($id_card);
        return idcard_checksum18($id_card);
    }else{
        return false;
    }
}
// 计算身份证校验码，根据国家标准GB 11643-1999
function idcard_verify_number($idcard_base){
    if(strlen($idcard_base)!=17){
        return false;
    }
    //加权因子
    $factor=array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
    //校验码对应值
    $verify_number_list=array('1','0','X','9','8','7','6','5','4','3','2');
    $checksum=0;
    for($i=0;$i<strlen($idcard_base);$i++){
        $checksum += substr($idcard_base,$i,1) * $factor[$i];
    }
    $mod=$checksum % 11;
    $verify_number=$verify_number_list[$mod];
    return $verify_number;
}
// 将15位身份证升级到18位
function idcard_15to18($idcard){
    if(strlen($idcard)!=15){
        return false;
    }else{
        // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
        if(array_search(substr($idcard,12,3),array('996','997','998','999')) !== false){
            $idcard=substr($idcard,0,6).'18'.substr($idcard,6,9);
        }else{
            $idcard=substr($idcard,0,6).'19'.substr($idcard,6,9);
        }
    }
    $idcard=$idcard.idcard_verify_number($idcard);
    return $idcard;
}
// 18位身份证校验码有效性检查
function idcard_checksum18($idcard){
    if(strlen($idcard)!=18){
        return false;
    }
    $idcard_base=substr($idcard,0,17);
    if(idcard_verify_number($idcard_base)!=strtoupper(substr($idcard,17,1))){
        return false;
    }else{
        return true;
    }
}


/**
* 验证手机号是否正确
* @author honfei
* @param number $mobile
*/
function isMobile($mobile) {
    if (!is_numeric($mobile)) {
        return false;
    }
    return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
}


/** 
     * 调用phpqrcode生成二维码 
     * @param string $url 二维码解析的地址 
     * @param int $level 二维码容错级别 
     * @param int $size 需要生成的图片大小 
     */  
    function qrcode($url = "", $level = 3, $size = 4)  
    {  
        Vendor('phpqrcode.phpqrcode');  
        //容错级别  
        $errorCorrectionLevel = intval($level);  
        //生成图片大小  
        $matrixPointSize = intval($size);  
        //生成二维码图片  
        $object = new \QRcode();  
        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件  
		 // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false  
        $path = "Public/Images/";  
        if(!file_exists($path))   
        {   
            mkdir($path, 0700);   
        }  
        // 生成的文件名  
		$username = "picture";
        $fileName = $path.$username.'.png';  
        ob_end_clean();//清空缓冲区 

        $object->png($url, $fileName, $errorCorrectionLevel, $matrixPointSize, 2);  
    }  

/*判断是否有48小时未提供帮助*/
function get_provide_help($name){
	
	/*查询用户交易记录*/
	$User = M ( 'user' ); // 实例化User对象
	$map['UE_account'] = $name;
	$infos = $User->where ( $map )->find ();
	/*is_yes 1开启判断 2关闭判断*/
	if($infos['is_yes'] == '1'){
		
		/*判断是否要封号*/
		$expiration_date = strtotime($infos['expiration_date']);
	
		if(time() > $expiration_date){
		   /*执行封号*/
		   $User->where(array('UE_account'=>$name))->save(array('UE_status'=>'1'));
		   return true;	
		}
	
	}
}
	

/*判断是否要封号*/
function get_fh($name){
	
	/*判断条件*/
	$no_pay = 0;
	$no_conf = 0;
	
	/*查询用户交易记录*/
	$User = M ( 'ppdd' ); // 实例化User对象
	$map['p_user']=$name;
	$map['zt'] = 0;
	$list = $User->where ( $map )->select ();
	
	$now_date = time(); //当前时间
	
	/*循环判断是否有超过时间未打款的*/
	if(!empty($list)){
		foreach($list as $key=>$value){
			$n_time = 0;
			$n_time = datedqsj($value['date']);
			$date = strtotime($n_time); //开始时间
			
			if($now_date > $date){
				$no_pay = 1;
				break;
			}
		
		}
	}
		
	$map2['g_user']=$name;
	$map2['zt'] = 1;
	$map2['ts_zt'] = 0; //投诉
	$list2 = $User->where ( $map2 )->select ();

	/*循环判断是否有超过时间未打款的*/
	if(!empty($list2)){
		foreach($list2 as $k=>$v){
			$ntime = 0;
			$ntime = datedqsj($v['date_hk']);
			$dates = strtotime($ntime); //开始时间
			
			if($now_date > $dates){
				$no_conf++;
				/*超时自动确认收款*/
				auto_receipt($v['id']);
				//break;
			}
		
		}
	}
	
	/*是否封号*/
    if($no_pay == 0 && $no_conf == 0){
		
		return false;
		
	}else{
		/*封号*/
		M('user')->where(array('UE_account'=>$name))->save(array('UE_status'=>'1'));
		return true;
	}	
	
}


/*绑定推荐人*/
function bdtuijian($name){
	
	if(!empty($name)){
		/*查询用户信息*/
		$users = M('user')->where(array('UE_account'=>$name))->find();
	
		/*是否已经有推荐人*/
		if(empty($users['ue_accname']) && !empty($users['temp_accname'])){
		
			/*更改推荐人*/
			M('user')->where(array('UE_account'=>$name))->save(array('UE_accName'=>$users['temp_accname']));
			
			/*更改推荐人 推荐人数 (直推人)推荐人等级 */
			mmtjnumadd($users['temp_accname']);
			
		}
		
	}
	
}

/*更新推荐会员数量*/
function mmtjnumadd($var){
	
	/*更新推荐会员数量*/
	M('user')->where(array('UE_account' => $var))->setInc('t_num',1);
	
	/*(直推人)推荐人等级 */
	$usermm=M('user')->where(array('UE_account'=>$var))->find();
	
	$numtemparr = explode(',',C("jjaccountnum"));
	$nametemparr = explode(',',C("jjaccountlevel"));
	$levelnum=0;
	
	// echo "<pre>";
	// print_r($numtemparr);
	// print_r($nametemparr);
	// echo "</pre>";
	// echo "<br/>";
	
	foreach($numtemparr as $k=>$num){
		
		if($usermm['t_num']>=$num){
			$levelnum=$k;
		}
		
	}
	
	// echo $levelnum;
	// echo "<br/>";
	$levelname = $nametemparr[$levelnum];
	
	// echo $levelname;
	M('user')->where(array('UE_account' => $var))->save(array('levelname' => $levelname,'UE_level' => $levelnum));
	
	return true;
	
}


/*超时自动确认收款*/
function auto_receipt($val){
	
	if(!empty($val)){
	    
		$ppddxx=M('ppdd')->where(array('id'=>$val,'zt'=>'1'))->find();
		
		M('ppdd')->where(array('id'=>$val,'zt'=>'1'))->save(array('zt'=>'2'));//更新此订单状态
					
		$txyqr=M('ppdd')->where(array('g_id'=>$ppddxx['g_id'],'zt'=>'2'))->sum('jb');
		
		$txzs=M('jsbz')->where(array('id'=>$ppddxx['g_id']))->find();
		if($txzs['jb']==$txyqr){
			M('jsbz')->where(array('id'=>$ppddxx['g_id']))->save(array('qr_zt'=>'1'));//提现订单已确认
		}
		
		$czyqr=M('ppdd')->where(array('p_id'=>$ppddxx['p_id'],'zt'=>'2'))->sum('jb');
		
		$czzs=M('tgbz')->where(array('id'=>$ppddxx['p_id']))->find();
		if($czzs['jb']==$czyqr){
			M('tgbz')->where(array('id'=>$ppddxx['p_id']))->save(array('qr_zt'=>'1'));//提现订单已确认
		}

		$lx_he = $ppddxx['jb'];
		$user_zq = M('user')->where(array('UE_account' => $ppddxx['p_user']))->find();
		M('user')->where(array('UE_account' => $ppddxx['p_user']))->setInc('UE_money', $lx_he);
		$user_xz = M('user')->where(array('UE_account' => $ppddxx['p_user']))->find();

		$note3 = "提供帮助本金";
		$record3["UG_account"] = $user_zq['ue_account']; // 登入轉出賬戶
		$record3["UG_type"] = 'jb';
		$record3["UG_allGet"] = $user_zq['ue_money']; // 金幣
		$record3["UG_money"] = '+' . $lx_he; //
		$record3["UG_balance"] = $user_xz['ue_money']; // 當前推薦人的金幣餘額
		$record3["UG_dataType"] = 'tgbz'; // 金幣轉出
		$record3["UG_note"] = $note3; // 推薦獎說明
		$record3["UG_getTime"] = date('Y-m-d H:i:s', time()); //操作時間
		$record3["varid"] = '0';
		$reg4 = M('userget')->add($record3);

	}
	
}

/*用户登录后每天返利息*/
function one_day_bzlx($name){
	
	/*查询当前登录用户利息表记录*/
	$map['g_user'] = $name;
	$map['frequency'] = array('lt',15); //还可以获取利息
	$list = M("tgbzlx")->where ( $map )->select ();
	
	if(!empty($list)){
		foreach($list as $key=>$value){
			
			/*查询信息*/
			$map2 = array();
			$map2['p_user'] =$value['g_user'];
			$map2['p_id'] =$value['bz_id'];
			$map2['zt'] = array('egt',1);
			$ppdd = M("ppdd")->where ($map2 )->find (); // 查詢滿足要求的總記錄數
			
			if(!empty($ppdd['id'])){
				/*已经付款了 判断付款时间以更改获得利息的比例*/
				$date = strtotime($ppdd['date']); //开始时间
				$date_hk = strtotime($ppdd['date_hk']); //打款时间
				
				// echo "开始时间".$date."<br/>";
				// echo "打款时间".$date_hk."<br/>";
				
				/*相差时间*/
				$hour = 0;
				$hour = $date_hk-$date;
				
				/*奖励比例*/
				$reward = 1;
				if($hour >= 0 && $hour <= 21600){
					$reward = C("jjdkwhattimejl1");	
				}elseif($hour > 21600 && $hour <= 64800){
					$reward = C("jjdkwhattimejl2");
				}elseif($hour > 64800 && $hour <= 86400){
					$reward = C("jjdkwhattimejl3");
				}
				
				// echo "相差小时数".$hour."<br/>";
				// echo "奖励比例".$reward."<br/>";
				
				/*更改获得利息的比例*/
				if($value['propor'] != $reward){
					M('tgbzlx')->where(array('id'=>$value['id']))->save(array('propor'=>$reward));
                }
			}
			
			/*更新利息表数据*/
			get_tgbzlx_day($value['id']);

		}
	}

}



/*每天返利息 更新利息数据 以及用户账户*/
function get_tgbzlx_day($lx_id){
	
	/*查询最后一次发利息的数据*/
	$map['lx_id'] = $lx_id;
	$tgbzlx = M("tgbzlx_log")->where ($map )->order('time desc')->find (); // 查詢滿足要求的總記錄數
	
	/*查询应该发几次利息*/
	$last_time = strtotime(date('Y-m-d', $tgbzlx['time']));//最后一次发利息时间
	$now_time  = strtotime(date('Y-m-d', time()));//当前时间
	
	$days = ($now_time-$last_time)/86400;
	
	if($days > 0){
		
		$ndays = intval($days);
		
		/*循环返利息*/
		for ($i=1;$i<=$ndays;$i++){
			
			/*查询利息信息*/
			$infos = array();
			$where['id'] = $lx_id;
			$infos = M("tgbzlx")->where ($where)->find ();
			
			/*更改利息表数据*/
			$frequency = $infos['frequency']*1+1;
			$time = date('Y-m-d H:i:s', time());
			M('tgbzlx')->where(array('id'=>$lx_id))->save(array('frequency'=>$frequency,'up_time'=>$time));
			
			/*更改用户账户信息 并记录log*/
			$lx_money = 0;
			$lx_money = $infos['amount']*$infos['propor']*0.01;
			tgbzlx_log($infos['g_user'],$lx_money,$lx_id,$infos['bz_id'],$frequency);
			
		}
		
	}
	
}



/*提供帮助15天利息*/
function tgbzlx($uname, $amount, $bz_id)
{
    $tgbz['g_user'] = $uname; //接受人
    $tgbz['add_time'] = date('Y-m-d H:i:s', time()); //操作時間
    $tgbz['bz_id'] = $bz_id; //提供帮助交易ID
    $tgbz['amount'] = $amount; //总金额
    $tgbz['propor'] = 1; //利息比例 默认 1%
    $tgbz['frequency'] = 1; //次数
    $tgbz['up_time'] = date('Y-m-d H:i:s', time()); //更改时间
    
    /*写入利息表*/
    $lx_id = M('tgbzlx')->add($tgbz);
   
    /*更改用户账户信息 并记录log*/
    if($lx_id){
		$lx_money = 0;
		$lx_money = $amount*1*0.01;
		tgbzlx_log($uname,$lx_money,$lx_id,$bz_id,1);
		
	}
	
}

/*更改用户账户信息 并记录log*/
function tgbzlx_log($uname,$lx_money,$lx_id,$bz_id,$frequency){
	
	/*写入利息log表*/
	$tgbzlx['g_user'] = $uname; //接受人
	$tgbzlx['lx_id'] = $lx_id; //LXid
	$tgbzlx['bz_id'] = $bz_id; //帮助交易ID
    $tgbzlx['add_time'] = date('Y-m-d H:i:s', time()); //操作時間
    $tgbzlx['desc'] = "第".$frequency."天获得利息"; //描述
    $tgbzlx['interest'] = $lx_money; //利息金额
    $tgbzlx['time'] = time(); //当前时间

    /*写入利息表*/
    M('tgbzlx_log')->add($tgbzlx);
	
	
	/*更改用户账户信息*/
	M('user')->where(array(UE_account => $uname))->setInc('UE_money',$lx_money);
	
}



?>